package abs_max

import "C"

// max: |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|

const MaxValue = 1e8
const MinValue = -1e8

var vectorX = []int{-1, -1, 1, 1, 1}
var vectorY = []int{-1, 1, -1, -1, 1}
var vectorZ = []int{-1, 1, 1 - 1, 1}

func maxAbsValExpr(arr1 []int, arr2 []int) int {
	n := len(arr1)
	var maxv, minv int
	var x, y, z int
	maxAbsVal := 0
	for d := 0; d < len(vectorX); d++ {
		maxv = MinValue
		minv = MaxValue
		for i := 0; i < n; i++ {
			x, y, z = i, arr1[i], arr2[i]
			maxv = max(maxv, x*vectorX[d]+y*vectorY[d]+z*vectorZ[d])
			minv = min(minv, x*vectorX[d]+y*vectorY[d]+z*vectorZ[d])
		}
		maxAbsVal = max(maxAbsVal, maxv-minv)
	}
	return maxAbsVal
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

var vector = [8][3]int{{1, 1, 1}, {1, 1, -1}, {1, -1, 1}, {-1, 1, 1}, {1, -1, -1}, {-1, -1, 1}, {-1, 1, -1}, {-1, -1, -1}}

func maxAbsValExpr2(arr1 []int, arr2 []int) int {
	length := len(arr1)
	maxAbsVal := 0
	for _, v := range vector {
		var maxT int = -1e8
		for i := 0; i < length; i++ {
			maxT = max(maxT, i*v[0]+arr1[i]*v[1]+arr2[i]*v[2])
			maxAbsVal = max(maxAbsVal, maxT-i*v[0]-arr1[i]*v[1]-arr2[i]*v[2])
		}
	}
	return maxAbsVal
}
